#ifndef _FACEDIR_H_
#define _FACEDIR_H_

#include <iostream>
#include <string.h>

#include <stdlib.h>
#include <stdio.h>

#include <sys/time.h>
#include <unistd.h>
#include <sys/select.h>

#include "view.h"
#include "model.h"
#include "createmodel.h"
#include "backup.h"
#include "read_video_thread.h"
#include "stereo.h"
#include "drawmodel.h"

#include "SMATModel.hh"
#include "display.hh"
#include "utils.hh"
#include <boost/circular_buffer.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/barrier.hpp>
#include <boost/thread/locks.hpp>

#define SHOW_VIEWS 2


class CFacedir {
 public:
    CALIBRATION_PARAMS_t calibration_params[2];
    std::string filemodel;
    std::string basefilename;
    std::string filemodelsmat;
    int nviews;
    CView2D view[2];
    CModel3D model;
    CView view_0_aux;
    
    CRead_Video_Thread reader;
    CCreateModel *creation;
    CBackup backup;

    int iniframe;
    int endframe;
    int wait;
    unsigned int frame;
    int frame2;
    bool quit;  
    int key;
    int graficar;

    FILE * fout ;
    int save_video;
    int save_pose;
    FILE * videofile;

    float *a, *b, *c, *x, *y, *z;

    struct corners_params_t cp;
    ransac_params_t ransac_global_params;

    CDrawModel dm;
    
    boost::thread *m_thread[2];
    boost::barrier *synchro3;
    boost::barrier *synchro2;
    boost::mutex m_mutex;


 public:
 CFacedir() : model(4),reader(30),backup(30)
    {
	nviews = 2;
	key =0;
	ransac_global_params = InitRansac();
	printf("---------------------\n\n");
    }

    int main(void);
    void facedir_merge();
    void facedir_control(IplImage **);
    void init(void);
    void readCalibration(std::string filename);
    void ResizeImages();
    void CreateModel();
    void ShowModels(IplImage **original, IplImage**color);
    void init_control();
    bool SaveModel(std::string, CvRect roi);

    void adapt_versions();

    //model creation stuff
    void on_mouse(int event, int x, int y, int flags, int id);
    void createModel_Manual();
    void MostrarModelo();
    std::string get_pathfile();
    void operator ()(int i);
    void start();
    void join();
    bool ReadModel(std::string basename, CvRect roi);

    friend class CCreateModel;
};


int bucle_cvWaitKey(int * wait);

#ifndef _MAIN_CPP_

extern char window[4][100];
extern char Model_SMAT[2][100];


#endif


#endif
